/*
* qiudot.com Inc.
* Copyright (c) 2019 All Rights Reserved.
* create by qiudot
* date:2019-10-15
*/
package com.lora.web;

import com.acooly.core.common.web.AbstractJQueryEntityController;
import com.acooly.core.common.web.support.JsonEntityResult;
import com.lora.communication.util.DoorUtil;
import com.lora.entity.EntranceGuard;
import com.lora.enums.EquipmentEnum;
import com.lora.service.EntranceGuardService;
import com.lora.service.EquipmentService;
import com.lora.util.ApplicationUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 门禁刷卡记录表 管理控制器
 * 
 * @author qiudot
 * Date: 2019-10-15 20:17:31
 */
@Controller
@RequestMapping(value = "/manage/lora/entranceGuard/entranceGuard")
public class EntranceGuardManagerController extends AbstractJQueryEntityController<EntranceGuard, EntranceGuardService> {

	{
		allowMapping = "*";
	}

	@SuppressWarnings("unused")
	@Autowired
	private EntranceGuardService entranceGuardService;

	@Autowired
	private EquipmentService equipmentService;

	@Autowired
	DoorUtil doorUtil;

	@ResponseBody
	@RequestMapping(value = "openDoor")
	public JsonEntityResult<String> openDoor(HttpServletRequest request, int number) {
		JsonEntityResult<String> result = new JsonEntityResult<>();
		Map<String,Object> map=new HashMap<>();
		map.put("type", EquipmentEnum.ACCSS_CONTROL.getValue());
		List<Map<String, Object>> query = equipmentService.query(map);
		if(query!=null&&query.size()>0){
			for (Map<String, Object> stringObjectMap : query) {
				Object position = stringObjectMap.get("parameter");
				if(position!=null&&position.toString().indexOf("-")==1){
					String doornum = position.toString().substring(0, 1);
					if(!doornum.equals(number+"")){
						continue;
					}
					Object eid = stringObjectMap.get("eid");
					// number 门号 1,2
					DoorUtil doorUtil=ApplicationUtil.getBean(DoorUtil.class);
					Object postion = position.toString().substring(2);
					if(postion==null){
						// 返回失败
						result.setSuccess(false);
						result.setMessage("未找到门禁IP信息");
						return result;
					}
					try {
						doorUtil.open(eid + "", postion.toString(), 60000);
					}catch (Exception e){
						// 返回失败
						result.setSuccess(false);
						result.setMessage("开门失败！");
						return result;
					}
					result.setSuccess(true);
					result.setMessage("开门成功！");
					return result;
				}
			}
		}
		// 返回失败
		result.setSuccess(false);
		result.setMessage("开门失败！");
		return result;
	}

	@ResponseBody
	@RequestMapping(value = "readHistoryData")
	public JsonEntityResult<String> readHistoryData(HttpServletRequest request) {
		JsonEntityResult<String> result = new JsonEntityResult<>();
		Map<String,Object> map=new HashMap<>();
		map.put("type", EquipmentEnum.ACCSS_CONTROL.getValue());
		List<Map<String, Object>> query = equipmentService.query(map);
		if(query!=null&&query.size()>0) {
			for (Map<String, Object> stringObjectMap : query) {
				String position = stringObjectMap.get("parameter").toString();
				doorUtil.getLogs(stringObjectMap.get("eid").toString(), position.substring(2), 60000);
			}

		}
		result.setSuccess(true);
		return result;
	}

}
